<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="合同编号" prop="contractNumber">
        <el-input v-model="queryParams.contractNumber" placeholder="请输入合同编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单编号" prop="order_no">
        <el-input v-model="queryParams.order_no" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customId">
        <el-select v-model="queryParams.customId" placeholder="请选择客户名称" clearable>
          <el-option v-for="dict in CustomList" :key="dict.id" :label="dict.customName" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="业务员" prop="salesManId">
        <el-select v-model="queryParams.salesManId" placeholder="请选择业务员" clearable>
          <el-option v-for="dict in dict.type.yewuyuan" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="送货方式" prop="shipping">
        <el-select v-model="queryParams.shipping" placeholder="请选择送货方式" clearable>
          <el-option v-for="dict in dict.type.scm_shipping_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="签订日期">
        <el-date-picker v-model="daterangeSigningDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="申请人" prop="applyId">
        <el-select v-model="queryParams.applyId" placeholder="请选择申请人" clearable>
          <el-option v-for="dict in dict.type.mytask_applicant" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker v-model="daterangeApplyDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="申请状态" prop="applyStatus">
        <el-select v-model="queryParams.applyStatus" placeholder="请选择申请状态" clearable>
          <el-option v-for="dict in dict.type.mytask_apply_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人" prop="auditorId">
        <el-select v-model="queryParams.auditorId" placeholder="请选择审核人" clearable>
          <el-option v-for="dict in dict.type.mytask_auditor" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核时间">
        <el-date-picker v-model="daterangeAuditTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.mytask_audit_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['scm:contract:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['scm:contract:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="合同编号" align="center" prop="contractNumber" />
      <el-table-column label="订单编号" align="center" prop="order_no" />
      <el-table-column label="客户名称" align="center" prop="custom_name">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.scm_custom_type" :value="scope.row.customId" />
        </template> -->
      </el-table-column>
      <el-table-column label="业务员" align="center" prop="salesManId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yewuyuan" :value="scope.row.salesManId" />
        </template>
      </el-table-column>
      <el-table-column label="送货方式" align="center" prop="shipping">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.scm_shipping_type" :value="scope.row.shipping" />
        </template>
      </el-table-column>
      <el-table-column label="签订日期" align="center" prop="signingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同金额" align="center" prop="totalAmount" />
      <el-table-column label="申请人" align="center" prop="applyId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mytask_applicant" :value="scope.row.applyId" />
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="applyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请状态" align="center" prop="applyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mytask_apply_status" :value="scope.row.applyStatus" />
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditorId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mytask_auditor" :value="scope.row.auditorId" />
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mytask_audit_status" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['scm:contract:edit']">详 情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改供应链SCM-销售管理-销售合同对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同编号" prop="contractNumber">
          <el-input v-model="form.contractNumber" placeholder="请输入合同编号" disabled />
        </el-form-item>
        <el-form-item label="订单编号" prop="order_no">
          <el-input v-model="form.order_no" placeholder="请输入订单编号" disabled />
        </el-form-item>
        <el-form-item label="客户名称" prop="customId">
          <el-select v-model="form.customId" placeholder="请选择客户名称" disabled>
            <el-option v-for="dict in CustomList" :key="dict.id" :label="dict.customName"
              :value="parseInt(dict.id)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务员" prop="salesManId">
          <el-select v-model="form.salesManId" placeholder="请选择业务员" disabled>
            <el-option v-for="dict in dict.type.yewuyuan" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="送货方式" prop="shipping">
          <el-select v-model="form.shipping" placeholder="请选择送货方式" disabled>
            <el-option v-for="dict in dict.type.scm_shipping_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收货地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入收货地址" disabled />
        </el-form-item>
        <el-form-item label="签订日期" prop="signingDate">
          <el-date-picker clearable v-model="form.signingDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择签订日期"
            disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请人" prop="applyId">
          <el-select v-model="form.applyId" placeholder="请选择申请人" disabled>
            <el-option v-for="dict in dict.type.mytask_applicant" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间" prop="applyDate">
          <el-date-picker clearable v-model="form.applyDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择申请时间"
            disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请状态" prop="applyStatus">
          <el-select v-model="form.applyStatus" placeholder="请选择申请状态" disabled>
            <el-option v-for="dict in dict.type.mytask_apply_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核人" prop="auditorId">
          <el-select v-model="form.auditorId" placeholder="请选择审核人" disabled>
            <el-option v-for="dict in dict.type.mytask_auditor" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable v-model="form.auditTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择审核时间"
            disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态" disabled>
            <el-option v-for="dict in dict.type.mytask_audit_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入合同金额" disabled />
        </el-form-item>
        <el-form-item label="审核意见" prop="auditorComments">
          <el-input v-model="form.auditorComments" placeholder="请输入审核意见" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(1)">通 过</el-button>
        <el-button type="primary" @click="submitForm(0)">驳 回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContract, getContract, delContract, addContract, updateContract } from "@/api/scm/contract";
import { listCustom } from "@/api/scm/custom";

export default {
  name: "Contract",
  dicts: ['scm_custom_type', 'mytask_audit_status', 'yewuyuan', 'mytask_apply_status', 'mytask_applicant', 'mytask_auditor', 'scm_shipping_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应链SCM-销售管理-销售合同表格数据
      contractList: [],
      //客户名称
      CustomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeSigningDate: [],
      // 备注时间范围
      daterangeApplyDate: [],
      // 备注时间范围
      daterangeAuditTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNumber: null,
        order_no: null,
        customId: null,
        salesManId: null,
        shipping: null,
        signingDate: null,
        applyId: null,
        applyDate: null,
        applyStatus: null,
        auditorId: null,
        auditTime: null,
        auditStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.handleCustom();
  },
  methods: {
    /** 查询供应链SCM-销售管理-销售合同列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeSigningDate && '' != this.daterangeSigningDate) {
        this.queryParams.params["beginSigningDate"] = this.daterangeSigningDate[0];
        this.queryParams.params["endSigningDate"] = this.daterangeSigningDate[1];
      }
      if (null != this.daterangeApplyDate && '' != this.daterangeApplyDate) {
        this.queryParams.params["beginApplyDate"] = this.daterangeApplyDate[0];
        this.queryParams.params["endApplyDate"] = this.daterangeApplyDate[1];
      }
      if (null != this.daterangeAuditTime && '' != this.daterangeAuditTime) {
        this.queryParams.params["beginAuditTime"] = this.daterangeAuditTime[0];
        this.queryParams.params["endAuditTime"] = this.daterangeAuditTime[1];
      }
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractNumber: null,
        order_no: null,
        customId: null,
        salesManId: null,
        shipping: null,
        address: null,
        signingDate: null,
        applyId: null,
        applyDate: null,
        applyStatus: null,
        auditorId: null,
        auditTime: null,
        auditStatus: null,
        totalAmount: null,
        auditorComments: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSigningDate = [];
      this.daterangeApplyDate = [];
      this.daterangeAuditTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //客户名称
    handleCustom() {
      listCustom().then(res => {
        this.CustomList = res.rows;
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应链SCM-销售管理-销售合同";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContract(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应链SCM-销售管理-销售合同";
      });
    },
    /** 提交按钮 */
    submitForm(status) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.auditStatus = status;
            if (status != 0) {
              this.form.applyStatus = 2;
            } else {
              this.form.applyStatus = 1;
            }
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除供应链SCM-销售管理-销售合同编号为"' + ids + '"的数据项？').then(function () {
        return delContract(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('scm/contract/export', {
        ...this.queryParams
      }, `contract_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

